package com.jzqf.harmonydemo.library.media;

import com.jzqf.harmonydemo.utils.LogUtil;
import ohos.app.Context;
import ohos.media.codec.Codec;
import ohos.media.codec.CodecDescriptionList;
import ohos.media.common.BufferInfo;
import ohos.media.common.Format;
import ohos.media.common.Source;
import ohos.media.player.Player;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.util.List;

public class MediaPlayer {
    private final String TAG = MediaPlayer.class.getSimpleName();
    private Codec codec;
    private Context context;
    private Player player;

    public MediaPlayer(Context context) {
        this.context = context;
    }

    public void init() {
        List<String> mimes = CodecDescriptionList.getSupportedMimes();
        for (String mime : mimes) {
            LogUtil.d(TAG, "支持的格式=" + mime);
        }
        //判断某设备是否支持指定MIME对应的解码器
        boolean decode = CodecDescriptionList.isDecodeSupportedByMime(Format.VIDEO_MPEG4);
        LogUtil.d(TAG, "解码" + decode);
        boolean encode = CodecDescriptionList.isEncodeSupportedByMime(Format.AUDIO_FLAC);
        LogUtil.d(TAG, "编码" + encode);
        //校验设备是否支持指定Format的编码器
        Format format = new Format();
        format.putStringValue(Format.MIME, Format.VIDEO_AVC);
        format.putIntValue(Format.WIDTH, 2560);
        format.putIntValue(Format.HEIGHT, 1440);
        format.putIntValue(Format.FRAME_RATE, 30);
        format.putIntValue(Format.FRAME_INTERVAL, 1);
        boolean decoderFormat = CodecDescriptionList.isDecoderSupportedByFormat(format);
        boolean encoderFormat = CodecDescriptionList.isEncoderSupportedByFormat(format);
        LogUtil.d(TAG, "decoderFormat=" + decoderFormat + ",encoderFormat=" + encoderFormat);

    }

    public void createDecoder() {
        codec = Codec.createDecoder();
        Format fmt = new Format();
        fmt.putStringValue(Format.MIME, Format.VIDEO_AVC);
        fmt.putIntValue(Format.WIDTH, 1920);
        fmt.putIntValue(Format.HEIGHT, 1080);
        fmt.putIntValue(Format.BIT_RATE, 392000);
        fmt.putIntValue(Format.FRAME_RATE, 30);
        fmt.putIntValue(Format.FRAME_INTERVAL, -1);
        codec.setCodecFormat(fmt);
        codec.registerCodecListener(new Codec.ICodecListener() {
            @Override
            public void onReadBuffer(ByteBuffer byteBuffer, BufferInfo bufferInfo, int trackId) {
                Format fmt = codec.getBufferFormat(byteBuffer);
            }

            @Override
            public void onError(int errorCode, int act, int trackId) {
                LogUtil.d(TAG, "errorCode=" + errorCode + ",act=" + act + ",trackId=" + trackId);
            }
        });
        codec.start();
    }

    public void start(String path) {
        try {
            player = new Player(context);
            File file = new File(path); // 根据实际情况设置文件路径
            FileInputStream fileInputStream = new FileInputStream(file);
            FileDescriptor fd = fileInputStream.getFD(); // 从输入流获取FD对象
            Source source = new Source(fd);
            player.setSource(source);
            player.prepare();//准备播放
        } catch (Exception e) {
            e.printStackTrace();
        }
        player.setPlayerCallback(new Player.IPlayerCallback() {
            @Override
            public void onPrepared() {

            }

            @Override
            public void onMessage(int i, int i1) {

            }

            @Override
            public void onError(int errorType, int errorCode) {
                LogUtil.d(TAG, "errorType=" + errorType + ",errorCode=" + errorCode);
            }

            @Override
            public void onResolutionChanged(int i, int i1) {

            }

            @Override
            public void onPlayBackComplete() {
                if (player != null) {
                    player.stop();
                    player = null;
                }
            }

            @Override
            public void onRewindToComplete() {

            }

            @Override
            public void onBufferingChange(int i) {

            }

            @Override
            public void onNewTimedMetaData(Player.MediaTimedMetaData mediaTimedMetaData) {

            }

            @Override
            public void onMediaTimeIncontinuity(Player.MediaTimeInfo mediaTimeInfo) {

            }
        });
    }

    public void stop() {
        codec.stop();
    }

    public void release() {
        if (codec != null) {
            codec.release();
        }
        if (player != null) {
            player.release();
            player = null;
        }
    }
}
